<?


abstract class UICtrlDocBase extends CoderDocBase implements IToolboxUICtrlDoc
{

	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$sName=''	string	What's this Parameter ?
	 * @param	$sPath=''	string	What's this Parameter ?
	 * @return	void
	 */
	public function UICtrlDocBase( $sName='', $sPath='' ) 
	{
		$this->CoderDocBase($sName,$sPath) ;
		
		// 子文档： 数据校验器 集合
		$aChildDoc = new JCAT_ArchiveDocument('DataVerifierCollection') ;
		$this->AddDocument($aChildDoc,'DataVerifierCollection') ;

		// 子文档集合 数组
		// $this->Set('DataVerifierDocs',array(),false) ;
		
		// 实现方式，初始值：实例化
		$this->Set('UICtrlImplementType',UICtrlDocBase::IMPTYPE_INSTANTIATION) ;
		
		// 
		$this->SetChanged(false,true) ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetObjctName()
	{
		return $this->Get('UICtrlName') ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetObjectClass()
	{
		return $this->Get('UICtrlKind') ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetKindIcon($nSize=16)
	{
		return ROOT_PATH.'Resource/Image/Controller.24x24.png' ;
	}
	
	/**
	 * 向文档中加入一个 数据校验文档
	 * 
	 * @access	public
	 * @param	$aDoc	IToolboxDataVerifierDoc		what's this
	 * @return	void
	 */
	public function AddDataVerifierDoc(IToolboxDataVerifierDoc $aDoc)
	{
		$aDVDocCollection = $this->GetDocument('DataVerifierCollection') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDVDocCollection) ;
		
		$aDVDocCollection->AddDocument($aDoc) ;
	}
	
	
	
	/**
	 * 删除一个 数据校验文档
	 * 
	 * @access	public
	 * @param	$aDoc	IToolboxDataVerifierDoc		what's this
	 * @return	void
	 */
	public function RemoveDataVerifierDoc(IToolboxDataVerifierDoc $aDoc)
	{
		$aDVDocCollection = $this->GetDocument('DataVerifierCollection') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDVDocCollection) ;
		
		$aDVDocCollection->RemoveDocument($aDoc) ;
	}
	
	
	/**
	 * 为 数据校验文档 创建一个 迭代器
	 * 
	 * @access	public
	 * @return	JCAT_ArrayIterator
	 */
	public function CreateDataVerifierDocIterator()
	{
		$aDVDocCollection = $this->GetDocument('DataVerifierCollection') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDVDocCollection) ;
		
		return $aDVDocCollection->CreateDocumentIterator() ;
	}

	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	int
	 */
	public function GetImplementType()
	{
		$nImType = $this->Get('UICtrlImplementType') ;
		
		JCAT_ASSERT::ASSERT_( in_array($nImType,array(self::IMPTYPE_DERIVATION,self::IMPTYPE_INSTANTIATION)),
		 				JCAT_Language::Sentence('意外的实现方式：%s',$nImType) ) ;
		
		 return $nImType ;
	}

	// 代码产生函数 ///////////////////////////////////////////////////////
	

	/**
	 * 将序列化以后的内容存入到 文档
	 * 
	 * @access	protected
	 * @param	$sPath				文件路径
	 * @param	$sByteContents		字节内容
	 * @param	$bAsBinary			作为二进制存储
	 * @return	bool
	 */
	protected function SaveToFile( $sPath, $sByteContents, $bAsBinary=false )
	{
		// 调用父类
		parent::SaveToFile( $sPath, $sByteContents, $bAsBinary ) ;
		
		// 载入  数据校验器 类
		$this->WirteUnmodifiableBlock($sPath,'Include Data Verifier Class',$this->CreateCode_ImportDVClass(),CoderDocBase::UnmodifiableBlockKind_Code) ;
		
		// 载入 父类
		$this->WirteUnmodifiableBlock($sPath,'Include Parent Class',$this->CreateCode_IncludeParentClass(),CoderDocBase::UnmodifiableBlockKind_Code) ;
			
			
		// 以派生 方式 实现
		if( $this->GetImplementType()==UICtrlDocBase::IMPTYPE_DERIVATION )
		{
			// 继承语句
			$this->WirteUnmodifiableBlock($sPath,'Extends',$this->CreateCode_Extends(),CoderDocBase::UnmodifiableBlockKind_Code,1) ;
			
			// 调用父类构造函数
			$this->WirteUnmodifiableBlock($sPath,'Init Parent Class',$this->CreateCode_InitParentClass(),CoderDocBase::UnmodifiableBlockKind_Code,2) ;
			
			// 创建并载入 数据校验器
			$this->WirteUnmodifiableBlock($sPath,'Data Verifier',$this->CreateCode_DataVerifier(),CoderDocBase::UnmodifiableBlockKind_Code,2) ;
			
		}
		
		// 以实例化 方式 实现
		else
		{
			// 调用父类构造函数
			$this->WirteUnmodifiableBlock($sPath,'Init Parent Class',$this->CreateCode_InitParentClass(),CoderDocBase::UnmodifiableBlockKind_Code) ;
			
			// 创建并载入 数据校验器
			$this->WirteUnmodifiableBlock($sPath,'Data Verifier',$this->CreateCode_DataVerifier(),CoderDocBase::UnmodifiableBlockKind_Code) ;
			
			
		}
		
		// 完成 返回 
		return true ;
	}
	
	public function CreateCode_ImportDVClass()
	{
		$arrClasses = array() ;
		
		$aIterator = $this->CreateDataVerifierDocIterator() ;
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$aDVDoc = $aIterator->Current() ;
			
			// DV 类 的名称
			$sClassName = $aDVDoc->GetDVClassName() ;
			
			// 载入 DV 类的代码
			if( !isset($arrClasses[$sClassName]) )
			{
				$arrClasses[$sClassName] = $aDVDoc->CreateCode_ImportClass() ;
			}
			
			$aIterator->Next() ;
		}
		
		return implode("\r\n",$arrClasses) ;
	}

	abstract public function CreateCode_IncludeParentClass() ;

	public function CreateCode_Extends()
	{
		$sParentClassName = $this->Get('UICtrlKind') ;
		return "	extends {$sParentClassName} " ;
	}

	public function CreateCode_InitParentClass()
	{
		$sParentClassName = $this->Get('UICtrlKind') ;
		$sName = $this->Get('UICtrlName') ;
		$sUIName = $this->Get('UICtrlUIName') ;
		$sUICtrlMainWidgetID = $this->Get('UICtrlMainWidgetID') ;
		
		$bGtkUICtrl = preg_match('/^JCAT_UIGtkCtrl/',$sParentClassName) ;
	
		// 以派生 方式 实现
		if( $this->GetImplementType()==UICtrlDocBase::IMPTYPE_DERIVATION )
		{
			if($bGtkUICtrl)
			{
				return "		\$this->{$sParentClassName}(__CLASS__,JCAT_Language::Sentence(\"{$sUIName}\"),'{$sUICtrlMainWidgetID}') ; " ;
			}
			
			else 
			{
				return "		\$this->{$sParentClassName}(__CLASS__,JCAT_Language::Sentence(\"{$sUIName}\")) ; " ;
			}
		}
		
		// 以实例化 方式 实现
		else
		{
			if($bGtkUICtrl)
			{
				return "\$aNewInstance = new {$sParentClassName}('{$sName}',JCAT_Language::Sentence(\"{$sUIName}\"),'{$sUICtrlMainWidgetID}') ; " ;
			}
			
			else 
			{
				return "\$aNewInstance = new {$sParentClassName}('{$sName}',JCAT_Language::Sentence(\"{$sUIName}\")) ; " ;
			}
		}
	}
	

	public function CreateCode_DataVerifier()
	{
		$arrDVs = array() ;
		if($this->GetImplementType()==UICtrlDocBase::IMPTYPE_DERIVATION)
		{
			$sVarName = '$this' ;
			$nTab = 2 ;
		}
		else
		{
			$sVarName = '$aNewInstance' ;
			$nTab = 0 ;			
		}
		
		$aIterator = $this->CreateDataVerifierDocIterator() ;
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$aDVDoc = $aIterator->Current() ;
			
			$arrDVs[]= $aDVDoc->CreateCode($sVarName,$nTab) ;
			
			$aIterator->Next() ;
		}
		
		return implode("\r\n",$arrDVs) ;
	}

	// 属性 //////////////////////////////////////////

	/**
	 * 实现方式：派生
	 * 
	 * @access	const
	 * @var		int
	 */
	const IMPTYPE_DERIVATION = 1 ;
	
	/**
	 * 实现方式：实例化
	 * 
	 * @access	const
	 * @var		int
	 */
	const IMPTYPE_INSTANTIATION = 2 ;
}
?>